import {post}  from '@/util/request'
import storage from '@/util/storage'
import {getAddress,getLocation} from '@/plugin/Map/Mapaddress'

export default {
  namespace:'history',
	namespaced:true,
  state:{
		isListLoading:false,
		isPathLoading:false,
		pathList:[],
		carlist:[],
		pathdata:{}
	},
  mutations:{
    changeListLoading(state,v){
			state.isListLoading = v;
		},
		changePathLoading(state,v){
			state.isPathLoading = v;
		},
		changePath(state,v){
			state.pathdata = v;
		},
		changeList(state,v){
			let carlist = new Set(v.map(x=>x.plateNumber));
			state.carlist = [...carlist];
			state.pathList = v;
		}
  },
  actions:{
    async reloadList({commit}, data) {
			commit('changeListLoading',true);
			let res = await post('/rtmonitor/historytrajectory/list',data);
			commit('changeList',res.data.data);
			commit('changeListLoading',false);
		},
		async reloadPath({commit}, form){
			commit('changePathLoading',true);
			let res = await post('/monitor/history/historyTrack',form);
			let data = res.data.data;
			data.driveAlarmVos.forEach(v=>{
				v.lng = v.startLng;
				v.lat = v.startLat;
			});
			data.gps.forEach(v=>{
				v.driverName = data.driverRtVo.driverRtVo;
				v.tel = data.driverRtVo.tel;
        v.headPicUrl = data.driverRtVo.headPicUrl;
			});
			for(let k in data.peccancyVos){
				let p = await getLocation(data.peccancyVos[k].area);
				if(p){
					data.peccancyVos[k].lng = p.lng;
					data.peccancyVos[k].lat = p.lat;
				}

			}
			commit('changePath',res.data.data);
			commit('changePathLoading',false);
		}
  }
};
